<template>
  <div class="timeline">
    <h2 class="year">...</h2>

    <el-timeline class="timeline" v-for="(time,index) in list" :key="index">
      <el-timeline-item v-for="(article,index) in time.currentArticles" :key="index" :timestamp="article.createdAt" placement="top">
        <el-card class="time_item">
          <div class="article-item" @click="toArticle(article.id)">
            <div class="article-image">
              <el-image :src="article.img ? $cloud + article.img : require('@/assets/img/placeholder.png')">
                <div slot="placeholder" class="image-slot">
                  加载中<span class="dot">...</span>
                </div>
              </el-image>
            </div>
            <div class="article-item-content">
              <h1 class="article-title">{{article.title}}</h1>
              <div class="article-description">{{article.description || '暂无描述~'}}</div>
            </div>
          </div>
        </el-card>
      </el-timeline-item>
      <h2 class="year"><span v-if="index == list.length-1">start </span>{{time.currentYear}}</h2>
    </el-timeline>
  </div>
</template>

<script>
export default {
  name: 'Timeline',
  data() {
    return {
      list: [],
    }
  },
  mounted() {
    this.getTimeline()
  },
  methods: {
    // 获取时间轴
    getTimeline() {
      // 加载动画
      var loadingInstance = this.$loading.service({
        target: document.querySelector('.timeline'),
        fullscreen: false
      })
      this.$api.httpTimeLine().then((res) => {
        if (res.code === 200) {
          this.list = res.data

          if (loadingInstance){
            loadingInstance.close() // 关闭加载动画
          }
        }
      })
    },
    goto(path) {
      this.$router.push(path)
    },
    toArticle(id) {
      this.goto('/article/' + id)
    },
  },
}
</script>

<style lang="scss" scoped>
.article-item {
  display: flex;
  align-items: stretch;
  padding-top: 12px;
  .article-image {
    flex-shrink: 0; // 不缩放
    // flex: 1;
    width: 200px;
    height: auto;
    background-color: $light-white;
    text-align: center;
    display: block;
    display: flex;
    align-items: center;
    justify-content: center;

    .image-slot {
      display: block;
    }

    & img {
      width: 100%;
      height: 100%;
      border-radius: 4px;
    }
  }
  .article-item-content {
    // flex: 2;
    display: flex;
    cursor: pointer;
    flex-direction: column;
    align-content: space-around;
    padding-left: 24px;
    color: $gray;
    .article-title {
      font-weight: bold;
      font-size: 20px;
      padding-bottom: 12px;
      margin: 0;
      flex: 1;
    }
    .article-description {
      flex: 1;
      font-size: 14px;
      color: $light-gray;
      margin: 12px 0 24px;
    }
    .article-info {
      flex: 1;
      font-size: 14px;
      color: $light-gray;
      span {
        margin-right: 20px;
        i {
          padding-right: 8px;
        }
      }
    }
  }
}
::v-deep .el-card__body {
  padding: 6px;
}

.timeline {
  width: 1200px;
  margin: 40px auto;

  // 年份
  .year {
    margin: 20px 0;
    color: $light-black;
    span {
      font-size: 14px;
    }
  }
}
@media screen and (max-width: 1200px) {
  .timeline {
    width: 100%;
  }

  .article-image {
    width: 120px !important;
  }
}
</style>